package com.apes.fn.fico.prepaid.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.fico.reimbursement.model.FeeType;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.Arith;
import com.apes.framework.util.DateUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Title: PrepaidExpenseItem
 * @Description: 待摊费用明细
 * @author: ZhengZeXian
 * @create: 2022/2/10 16:33
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PrepaidExpenseItem.CONST_TABLE_NAME)
public class PrepaidExpenseItem extends BaseModel {

    public static final String CONST_TABLE_NAME = "fn_prepaid_expense_item";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "待摊费用明细", padding = 7)
    @Comment("待摊费用明细编码")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "prepaid_expense_id", referencedColumnName = "id")
    @Comment("待摊费用")
    private PrepaidExpense prepaidExpense;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fee_type_id", referencedColumnName = "id")
    @Comment("费用类型")
    private FeeType feeType;

    @Column(name = "type", nullable = false)
    @Comment("类型(0:门店;1:总部;2:其他)")
    private int type;

    @Column(name = "prepaid_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("待摊金额")
    private double prepaidAmount = 0;

    @Column(name = "prepaid_month", nullable = false, length = 2)
    @Comment("待摊月份")
    private Integer prepaidMonth;

    @Column(name = "allocated_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("已分摊金额")
    private double allocatedAmount = 0;

    @Column(name = "surplus_prepaid_month", nullable = false, length = 2)
    @Comment("剩余待摊月份")
    private Integer surplusPrepaidMonth;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Column(name = "payment_date")
    @Comment("付款日期")
    private Date paymentDate;

    @Comment("摘要")
    @Column(name = "summary")
    private String summary;

    @Comment("已分摊明细")
    @OneToMany(mappedBy = "prepaidExpenseItem", cascade = CascadeType.ALL)
    private List<ShareDetail> shareDetails;

    public List<ShareDetail> getShareDetails() {
        if (Objects.isNull(shareDetails)) return new ArrayList<>();
        for (ShareDetail item : shareDetails) {
            item.setPrepaidExpenseItem(this);
        }
        return shareDetails;
    }

    /**
     * 计算此次需要分摊金额
     * @param type 0：一次性分摊 1：按月分摊
     * @return 此次需要分摊金额
     */
    public Double computeShareAmount(int type){
        //总分摊金额 / 需要分摊月份
        double monthShare = Arith.div(this.prepaidAmount, this.prepaidMonth, 2);
        if (type == 0 || this.surplusPrepaidMonth == 0){
            return Arith.round(prepaidAmount - allocatedAmount, 2);
        }else{
            return monthShare;
        }
    }

}
